﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace Zero.Dialogue
{
    public class UI_BubblePanel : MonoBehaviour
    {
        public Text characterNameText;
        public Text bodyText;

        #region 事件

        public event EventHandler OnBubbleStart;
        public event EventHandler OnBubbleComplete;

        #endregion

        private Vector3 _offset = Vector3.up; // 位置偏移
        private float _duration = 1f; // 气泡的持续时间
        private float _lastShowTime; // 最后显示气泡的时间
        private GameObject _owner; // 绑定的对象
        private Camera _mainCamera;

        /// <summary>
        /// 绑定角色数据
        /// </summary>
        /// <param name="character">带气泡的角色</param>
        public void Bind(BubbleCharacter character)
        {
            _mainCamera = Camera.main;

            _owner = character.gameObject;
            SetOffset(character.bubbleOffset);
            SetCharacterName(character.characterName, character.outlineColor);
        }

        /// <summary>
        /// 设置气泡位置偏移
        /// </summary>
        /// <param name="offset">位置偏移</param>
        public void SetOffset(Vector3 offset)
        {
            _offset = offset;
        }

        /// <summary>
        /// 设置角色名
        /// </summary>
        /// <param name="characterName">角色名</param>
        /// <param name="outlineColor">轮廓颜色</param>
        public void SetCharacterName(string characterName, Color outlineColor)
        {
            characterNameText.text = characterName;
            var outline = characterNameText.GetComponent<Outline>();
            outline.effectColor = outlineColor;
        }


        /// <summary>
        /// 设置气泡主体
        /// </summary>
        /// <param name="body">主体内容</param>
        public void SetBody(string body)
        {
            bodyText.text = body;
        }

        /// <summary>
        /// 设置气泡持续时间
        /// </summary>
        /// <param name="duration">持续时间</param>
        public void SetDuration(float duration)
        {
            _duration = duration;
        }

        /// <summary>
        /// 显示气泡
        /// </summary>
        public void Show()
        {
            gameObject.SetActive(true);
            _lastShowTime = Time.time;

            OnBubbleStart?.Invoke(this, EventArgs.Empty);
        }

        /// <summary>
        /// 隐藏气泡
        /// </summary>
        public void Hide()
        {
            if (IsShow()) OnBubbleComplete?.Invoke(this, EventArgs.Empty);
            gameObject.SetActive(false);
        }

        /// <summary>
        /// 是否显示中
        /// </summary>
        /// <returns></returns>
        public bool IsShow()
        {
            return gameObject.activeSelf;
        }

        private void Update()
        {
            if (gameObject.activeSelf && _duration >= 0f)
            {
                if (Time.time >= _lastShowTime + _duration)
                {
                    gameObject.SetActive(false);
                    OnBubbleComplete?.Invoke(this, EventArgs.Empty);
                }
            }
        }

        private void LateUpdate()
        {
            // 跟随对象
            if (_owner != null)
            {
                var screenPoint = _mainCamera.WorldToScreenPoint(_owner.transform.position + _offset);
                transform.position = screenPoint;
            }
            else
            {
                // 绑定对象销毁时, 同时把对话气泡也销毁了
                Destroy(gameObject);
            }
        }
    }
}